package NsingCHM.Files;

import java.util.*;

/** 
 Class1 ��ժҪ˵����
*/
public class UpFile
{
	public UpFile()
	{
	}


	/** 
	 
	 
	 @param Type Ҫ�ϴ����ļ��������ơ��������ԣ����
	 /// @param fileAllName �ͻ����ļ�·�����ļ�����
	 @param FileName Ҫ�ϴ����ļ���
	 @param FilePath Ҫ��ŵ��ļ�·��
	 @return 
	*/
	public final boolean UpFiles(System.Web.UI.HtmlControls.HtmlInputFile File, ArrayList TypeAr, String Type, String FilePath, String FileName)
	{
		//System.Web.UI.HtmlControls.HtmlInputFile File = new System.Web.UI.HtmlControls.HtmlInputFile();
//			int count = fileAllName.LastIndexOf(".");
//			string type = fileAllName.Substring(count);

		if (!TypeJudge(TypeAr, Type))
		{
			return false;
		}

		try
		{
			File.PostedFile.SaveAs(FilePath + FileName + ".txt"); // System.Web.HttpContext.Current.Server.MapPath( "\\upfile\\" )   );
		}
		catch (RuntimeException e)
		{
			NsingCHM.Common.Tips.Alert(e.toString());
		}

	 //	File.PostedFile.SaveAs( System.Web.HttpContext.Current.Server.MapPath( "\\" + FilePath + "\\" )  + FileName ); 
		//File.PostedFile.SaveAs( System.Web.HttpContext.Current.Server.MapPath( "\\upfile" ) ); 

		return true;
	}

	public final String readFromServer(String FilePath, String FileName)
	{


		StreamReader sr = new StreamReader("e:\\a\\" + FileName, System.Text.Encoding.GetEncoding("GB2312"));
		String s = sr.ReadToEnd();
		sr.Close();
		return s;

	}


	private boolean TypeJudge(ArrayList TypesAr, String Type)
	{
		for (int i = 0; i < TypesAr.size(); i++)
		{
			if (TypesAr.get(i).toString().equals(Type))
			{
				return true;
			}
		}
		return false;
	}
	public final boolean ReadTxt(ArrayList TypeAr, String FilePath, String FileName, tangible.RefObject<String> ReadStr, String Type)
	{
		ReadStr.argValue = "";
		if (!TypeJudge(TypeAr, Type))
		{
			return false;
		}

		ReadStr.argValue = GetTxtStr(FilePath, FileName);
		return true;
	}
	private String GetTxtStr(String FilePath, String FileName)
	{
		StreamReader sr;
		String str = "";
		try
		{
			//sr = new StreamReader(FileAllPath,System.Text.Encoding.GetEncoding("GB2312"));
			sr = new StreamReader(FilePath + FileName,System.Text.Encoding.GetEncoding("GB2312"));
			str = sr.ReadToEnd();
		}
		catch (RuntimeException ee)
		{
			MOLE.App.Log.Add("read file err", ee.toString());
			return "";
		}
		sr.Close();
		return str;
	}
}